﻿using MyProject.Core;
using MyProject.Services.ListItem;
using System.Windows.Controls;
using UserEntity = MyProject.Entities.User.User;
using ListItemEntity = MyProject.Entities.ListItem.ListItem;
using LiveCharts;
using LiveCharts.Wpf;
using LiveCharts.Defaults;

namespace MyProject.ViewsModel.Chart
{
    public class ChartDialogContext
    {
        public ListItemService Lists { get; set; }

        public ChartDialogContext()
        {
            this.Lists = ListItemService.getInstance();

        }

        /// <summary>
        ///  Расчет параметров доп задания
        /// </summary>
        /// <param name="c1"> Объект для вывода кол-ва чел с загран паспортом </param>
        /// <param name="c2"> Объект диаграммы кол-ва чел с паспортом РФ </param>
        /// <param name="c3"> Объект диаграммы кол-ва женщин, которые поменяли паспорт </param>
        /// <param name="chart"> Объект диаграммы </param>

        public async void Calculate(Label c1, Label c2, PieChart chart)
        {
            
            List<UserEntity> userList;

            await using (MyDbContext db = new MyDbContext())
            {
                userList = db.User.ToList();
            }

            int ic1 = 0;
            int ic2 = 0;

            using (MyDbContext db = new MyDbContext())
            {
                var people = (from user in db.User
                              join place_residence in db.Addresses on user.place_residenceId equals place_residence.id
                              join registration in db.Addresses on user.registrationId equals registration.id
                             
                              where
                                place_residence.city_id != registration.city_id
                                ||
                                place_residence.street_id != registration.street_id
                                ||
                                place_residence.house != registration.house

                              select user).ToList();


                ic2 = people.Count();
            }

            using (MyDbContext db = new MyDbContext())
            {
                var people = (from user in db.User
                              join contract in db.EmploymentContract on user.contractId equals contract.id
                              join place_residence in db.Addresses on user.place_residenceId equals place_residence.id
                              join registration in db.Addresses on user.registrationId equals registration.id
                              where
                                contract.employment_date.Value.Year == 2015
                              where
                                place_residence.city_id == 3
                              where
                                registration.city_id == 3
                              select user).ToList();


                ic1 = people.Count();
            }

            Dictionary<int, int> listYears = new Dictionary<int, int>();
            using (MyDbContext db = new MyDbContext())
            {
                DateTime left = new DateTime(1999,12,31);
                DateTime right = new DateTime(2011,1,1);

                var years = (from user in db.User
                                 join passport in db.Passport on user.passportId equals passport.id
                                 where
                                    passport.date_of_issue != null
                                where
                                    passport.date_of_issue > left
                                where
                                    passport.date_of_issue < right
                                orderby passport.date_of_issue descending
                                select passport.date_of_issue.Value.Year).ToList();

                var people = db.User.ToList();

                    foreach (var year in years)
                    {
                       if (listYears.ContainsKey(year))
                        {
                            listYears[year]++;
                        } else
                        {
                            listYears.Add(year, 1);
                        }
                    }

                    if (listYears.Count > 0)
                    {
                        SeriesCollection collection = new SeriesCollection();

                        foreach (var year in listYears)
                        {
                            collection.Add(new PieSeries
                            {
                                Title = $"Получили паспорт в {year.Key}",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(year.Value) },
                            });
                        }

                    collection.Add(new PieSeries
                    {
                        Title = $"Остальные",
                        Values = new ChartValues<ObservableValue> { new ObservableValue(people.Count - years.Count) },
                        
                    });

                    chart.Series = collection;
                    }

                }

            c1.Content = ic1;
            c2.Content = ic2;
            
        }
    }
}
